package com.starblink.search.category.ui.main.tab

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.apollographql.apollo3.api.Optional
import com.starblink.android.basic.base.BaseViewModel
import com.starblink.android.basic.bean.loading.GoodsCardLoadingModel
import com.starblink.android.basic.config.PAGE_SIZE
import com.starblink.android.basic.data.model.product.ProductFWrap
import com.starblink.android.basic.ext.string
import com.starblink.android.basic.extension.apollo.toWrap
import com.starblink.android.basic.extension.lazyOnNone
import com.starblink.android.basic.network.base.SkGqlClient
import com.starblink.basic.apollo.ext.optional
import com.starblink.basic.style.R
import com.starblink.library.widget.loading.GoodsCardLoadingCell
import com.starblink.rocketreserver.FetchCategoryRecommendQuery
import com.starblink.rocketreserver.FetchRecommendProductListByForegroundCategoryIdQuery
import com.starblink.rocketreserver.FetchSaleZoneForegroundCategoryQuery
import com.starblink.rocketreserver.FetchSaleZoneForegroundCategoryV2Query
import com.starblink.rocketreserver.FetchSaleZoneProductRecommendQuery
import com.starblink.rocketreserver.fragment.CategoryF
import com.starblink.rocketreserver.type.CategoryRecommendReq
import com.starblink.rocketreserver.type.PageQueryReq
import com.starblink.rocketreserver.type.RecommendProduct4ForegroundCategoryReq
import com.starblink.rocketreserver.type.SaleZoneCategoryReq
import com.starblink.search.category.data.CategoryLevelEnum
import com.starblink.search.category.data.MainPlaceThreeModel
import com.starblink.search.result.repository.model.GoodsResultEmptyModel
import kotlinx.coroutines.launch

internal class CategoryTabVM() : BaseViewModel() {

    val dataLD = MutableLiveData<List<ProductFWrap>?>()

    val categoryLD = MutableLiveData<MainPlaceThreeModel>()

    private val threeCategoryMode = MainPlaceThreeModel(
        loading = true,
    )

    val emptyModel: GoodsResultEmptyModel by lazyOnNone {
        GoodsResultEmptyModel(
            emptyTxt1 = string(R.string.default_empty_text1),
            emptyTxt2 = string(R.string.search_empty_tip4)
        )
    }

    var categoryId: String? = null
    var categoryName: String? = ""

    var data = arrayListOf<Any>()

    var pageNo = 1

    var productIndexTag = 0


    init {
        data.add(threeCategoryMode)
        data.addAll(GoodsCardLoadingCell.LOADING_DATA)
    }

    fun queryCategories() {
        categoryId ?: return
        viewModelScope.launch {
            var list: List<CategoryF>? = null
            if ("0" == categoryId) {
                list = SkGqlClient.skQuery(
                    FetchSaleZoneForegroundCategoryV2Query(
                        params = SaleZoneCategoryReq(
                            level = Optional.present(3),
                        )
                    )
                )?.data?.fetchSaleZoneForegroundCategoryV2?.map {
                    val categoryD = it?.categoryD
                    CategoryF(
                        categoryD?.id,
                        categoryD?.name,
                        categoryD?.level,
                        categoryD?.imageUrl,
                        categoryD?.littleImageUrl,
                        categoryD?.existsLowerLevel,
                        categoryD?.iconUrl,
                        categoryD?.traceId,
                        categoryD?.traceInfo,
                        categoryD?.highestDiscountInfo
                    )
                }
            } else {
                list = SkGqlClient.skQuery(
                    FetchCategoryRecommendQuery(
                        params = CategoryRecommendReq(
                            foregroundCategoryId = categoryId.optional(),
                            scene = CategoryLevelEnum.THREE.level.optional(),
                        )
                    )
                )?.data?.fetchCategoryRecommend?.map { it.categoryF }
            }

            categoryLD.postValue(threeCategoryMode.apply {
                loading = list.isNullOrEmpty()
                categories = list
            })
        }
    }

    fun queryGoods() {
        categoryId ?: return
        viewModelScope.launch {
            if (pageNo == 1) {
                productIndexTag = 0
            }
            var list: List<ProductFWrap>? = null

            if ("0" == categoryId) {
                list = SkGqlClient.skQuery(
                    FetchSaleZoneProductRecommendQuery(
                        param = PageQueryReq(
                            pageNo = pageNo,
                            pageSize = PAGE_SIZE.optional(),
                        )
                    )
                )?.data?.fetchSaleZoneProductRecommend?.mapIndexed { index, it ->
                    it.productF.toWrap(
                        index + productIndexTag
                    )
                }
            } else {
                list = SkGqlClient.skQuery(
                    FetchRecommendProductListByForegroundCategoryIdQuery(
                        param = RecommendProduct4ForegroundCategoryReq(
                            foregroundCategoryId = categoryId!!,
                            pageNo = pageNo,
                            pageSize = PAGE_SIZE.optional(),
                        )
                    )
                )?.data?.fetchRecommendProductListByForegroundCategoryId?.mapIndexed { index, it ->
                    it.productF.toWrap(
                        index + productIndexTag
                    )
                }
            }

            productIndexTag += list?.size ?: 0

            if (pageNo == 1 && list?.isNotEmpty() == true) {
                //data.removeWhen { goodsLoadingList.contains(it) }
                data.clear()
                data.add(threeCategoryMode)

//                data.add(emptyModel)
            }
            dataLD.postValue(list)
        }
    }


}